package Control;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import Entity.*;
import GUI.*;
/**
 * This class handle the meeting for today massage for the manager and send the given information from the GUI's class to the server
 * it's create an event, cancel event, edit event and close an event
 * @author aviat
 */
public class HandleMassage {

	private ViewMsgGui massageGui;
	private ViewMsgGui msgGuiNext;
	private MsgPackage handleMassages;
	private ArrayList<Massage> msgList;
	
	private int msgNextWindow;
	/**
	 * Constructor
	 */
	public HandleMassage() {
		
		massageGui = new ViewMsgGui();
		msgGuiNext = new ViewMsgGui();
		msgList = new ArrayList<>();
		msgNextWindow=0;
		handleMassages = new MsgPackage();
	}
	/**
	 * This method sent a request to server to get massage for the Manger that login 
	 * with the meeting of today
	 */
	public void getMsg(){
		
		handleMassages.setObject(ScreenManager.getUserLogged().getUserName());
		handleMassages.setOp_code(OP_CODE.GET_MSG);
		ScreenManager.getConnection().accept(handleMassages);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		msgList =(ArrayList<Massage>) o;
		if(getMsgLenght()!=0)
			JOptionPane.showMessageDialog(null, "You have "+getMsgLenght()+" new massages, Please check them by click massage button ", "Massage", JOptionPane.INFORMATION_MESSAGE);
			
	}
	/**
	 * This method set massage Gui details to and display it
	 */
	public void setMsg1Gui(){

		if(msgNextWindow==getMsgLenght()-1)
			massageGui.getBtnNext().setEnabled(false);
				
		else if (getMsgLenght()==1)
			massageGui.getBtnNext().setEnabled(false);
		else
			massageGui.getBtnNext().setEnabled(true);
		
		Massage m = msgList.get(getMsgNextWindow());
			
			massageGui.geteDateTextField().setText(m.geteDate());
			massageGui.getcNametextField().setText(m.getCustName());
			massageGui.getNumMeetingTextField().setText(m.getNumOfMeeting().toString());
			massageGui.getmTimeTextField().setText(m.getmTime());	
	}
	/**
	 * This method set massage Gui details to and display it
	 */
	public void setMsg2Gui(){
		
		if(msgNextWindow==getMsgLenght()-1)
			msgGuiNext.getBtnNext().setEnabled(false);
				
		else if (getMsgLenght()==1)
			msgGuiNext.getBtnNext().setEnabled(false);
		else
			msgGuiNext.getBtnNext().setEnabled(true);
			
			Massage m = msgList.get(getMsgNextWindow());
			
			msgGuiNext.geteDateTextField().setText(m.geteDate());
			msgGuiNext.getcNametextField().setText(m.getCustName());
			msgGuiNext.getNumMeetingTextField().setText(m.getNumOfMeeting().toString());
			msgGuiNext.getmTimeTextField().setText(m.getmTime());
	}
	
	/**
	 * @return the msgGuiNext
	 */
	public ViewMsgGui getMsgGuiNext() {
		return msgGuiNext;
	}

	public int getMsgLenght(){
		if(msgList.isEmpty())
			return 0;
		return msgList.size();
	}

	/**
	 * @return the msgNextWindow
	 */
	public int getMsgNextWindow() {
		return msgNextWindow;
	}

	/**
	 * @param msgNextWindow the msgNextWindow to set
	 */
	public void setMsgNextWindow(int msgNextWindow) {
		this.msgNextWindow = msgNextWindow;
	}

	/**
	 * @return the msgList
	 */
	public ArrayList<Massage> getMsgList() {
		return msgList;
	}

	/**
	 * @param msgList the msgList to set
	 */
	public void setMsgList(ArrayList<Massage> msgList) {
		this.msgList = msgList;
	}

	/**
	 * @return the massageGui
	 */
	public ViewMsgGui getMassageGui() {
		return massageGui;
	}

		/**
	 * @param massageGui the massageGui to set
	 */
	public void setMassageGui(ViewMsgGui massageGui) {
		this.massageGui = massageGui;
	}
}
